﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TransportLayer;

namespace VirtualOperator
{
    public partial class MainGUI : Form
    {
        public static DateTime startTime;
        
        VirtualNetworkOperator vno;
        public MainGUI()
        {
            vno = new VirtualNetworkOperator();
            InitializeComponent();
            vno.Log.LogAppendHanlder += new LogAppend(PrintLog);
            vno.Init();
            virtualNetworksListBox.DataSource = vno.BindingVirtualNetworks;
            accesibleElementsListBox.DataSource = vno.BindingAccessibleMatrixes;
            
        }

        private void PrintLog(object sender, string message)
        {
            try
            {
                outputTextBox.Text += message + Environment.NewLine;
            }
            catch (Exception) { }
            outputTextBox.SelectionStart = outputTextBox.Text.Length;
            outputTextBox.ScrollToCaret();
        }

     
 
        private void button1_Click(object sender, EventArgs e)
        {
            //BindingList<int> sss;
        }


        public void deleteNetworkConnection(UserConnection nc)
        {
            this.RefreshRoutes();

        }



        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        public void RefreshRoutes()
        {
            routesListBox.DataSource = vno.CurrentVirtualNetwork.UserConnectionsDictonary.Values.ToList();
            routesListBox.Refresh();
        }

        public void RefreshUsers()
        {
            virtualUsersLinkBox.DataSource = vno.CurrentVirtualNetwork.NwTTPDictonary.Keys.ToList();
            virtualUsersLinkBox.Refresh();
        }

        private void RefresAccessibleMatrixes()
        {
            accesibleElementsListBox.DataSource = vno.AccessibleMatrixes.Values.ToList();
        }
        private void refreshButton_Click(object sender, EventArgs e)
        {
            this.Refresh();
        }

        public override void Refresh()
        {
            base.Refresh();

            if (vno.Communicator.Connected)
            {
                connectionStateLabel.Text = "on";
                
            }
            else { 
                connectionStateLabel.Text = "off";
               
            }
        }

        private void RefreshVirtualNetworks()
        {
            virtualNetworksListBox.DataSource = vno.VirtualNetworks.Values.ToList();
        
        }


        private void routesListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (routesListBox.SelectedValue != null && topologyViewer1.LoadFalg)
            {
                topologyViewer1.ColorNetworkConnection(routesListBox.SelectedValue as UserConnection);
                outputTextBox.Text = routesListBox.SelectedValue.ToString();
                //toDO
            }
        }

        private void saveConfigurationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.PROPERTY);
            wn.Show();
        }

        private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.VIRTUAL_NETWORK);
            wn.Show();  

        }

        private void connectToPicsNCToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.PROPERTY);
            wn.Show();
        }

        private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.PROPERTY);
            wn.Show();

        }

        private void addVirtuallinkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VirtualNetworkTools wn = new VirtualNetworkTools(vno,TabPageEnum.ADD_VIRTUAL_LINK);
            wn.Show();
        }

        private void loadConfigurationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.PROPERTY);
            wn.Show();
        }

        private void addSinkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.ADD_SINK);
            wn.Show();
        }

        private void addSourceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.ADD_SOURCE);
            wn.Show();
        }

        private void setupConnectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.SETUP);
            wn.Show();
        }

        private void teardownConnectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.TEARDOWN);
            wn.Show();
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            TopologyForm topForm = new TopologyForm(vno);
            topForm.Show();
        }

        private void virtualNetworksListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            vno.CurrentVirtualNetwork = virtualNetworksListBox.SelectedValue as VirtualNetwork;
            if (vno.CurrentVirtualNetwork != null)
            { 
                topologyViewer1.LoadTopology(vno.CurrentVirtualNetwork);
                routesListBox.DataSource = vno.CurrentVirtualNetwork.UserConnections;
                virtualUsersLinkBox.DataSource = vno.CurrentVirtualNetwork.Users;
                
            }
            this.Refresh();
        }

    

        private void MainGUI_FormClosed(object sender, FormClosedEventArgs e)
        {
            vno.Clean();
        }

        private void detailButton_Click(object sender, EventArgs e)
        {
            outputTextBox.Text = "";

        }

        private void openConsoleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConsolGUI console = new ConsolGUI(vno);
            console.Show();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("VNO v0.9\nMore about this aplication at: http://code.google.com/p/pi-cs/");
        }

        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click_2(object sender, EventArgs e)
        {

        }

        private void accesibleElementsListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var matrix = (VirtualNetwork.Matrix)accesibleElementsListBox.SelectedItem;
            
            if(!matrix.Open)
            {
                NodeDetails nodeDetail = new NodeDetails(matrix);
                nodeDetail.Show();
            
            }
            
        }

        private void saveLoadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.SAVE);
            wn.Show();
        }

        private void teardownConnectionToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.TEARDOWN);
            wn.Show();
        }

        private void connectPictureBox_Click(object sender, EventArgs e)
        {
            var pictureButton = sender as PictureBox;
            pictureButton.BorderStyle = BorderStyle.Fixed3D;
            if (vno.ConnectToPicsNC())
            {
                vno.GetMatrixes();
                connectionStateLabel.Text = "on";
                this.Refresh();
            }
            else connectionStateLabel.Text = "off";

            pictureButton.BorderStyle = BorderStyle.None;
        }

        private void addVNPictureBox_Click(object sender, EventArgs e)
        {
            var pictureButton = sender as PictureBox;
            pictureButton.BorderStyle = BorderStyle.Fixed3D;
            if (vno.Communicator.Connected)
            {
                VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.VIRTUAL_NETWORK);
                wn.Show();
            }
            else connectionStateLabel.Text = "off";

            pictureButton.BorderStyle = BorderStyle.None;
        }

        private void addVLpictureBox_Click(object sender, EventArgs e)
        {
            var pictureButton = sender as PictureBox;
            pictureButton.BorderStyle = BorderStyle.Fixed3D;
            if (vno.Communicator.Connected)
            {
                VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.ADD_VIRTUAL_LINK);
                wn.Show();
            }
            else connectionStateLabel.Text = "off";

            pictureButton.BorderStyle = BorderStyle.None;
        }

        private void addSinkPictureBox_Click(object sender, EventArgs e)
        {
            var pictureButton = sender as PictureBox;
            pictureButton.BorderStyle = BorderStyle.Fixed3D;
            if (vno.Communicator.Connected)
            {
                VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.ADD_SINK);
                wn.Show();
            }
            else connectionStateLabel.Text = "off";

            pictureButton.BorderStyle = BorderStyle.None;
        }

        private void addSourcePictureBox_Click(object sender, EventArgs e)
        {
            var pictureButton = sender as PictureBox;
            pictureButton.BorderStyle = BorderStyle.Fixed3D;
            if (vno.Communicator.Connected)
            {
                VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.ADD_SOURCE);
                wn.Show();
            }
            else connectionStateLabel.Text = "off";

            pictureButton.BorderStyle = BorderStyle.None;
        }

        private void setupPictureBox_Click(object sender, EventArgs e)
        {
            var pictureButton = sender as PictureBox;
            pictureButton.BorderStyle = BorderStyle.Fixed3D;
            if (vno.Communicator.Connected)
            {
                VirtualNetworkTools wn = new VirtualNetworkTools(vno, TabPageEnum.SETUP);
                wn.Show();
            }
            else connectionStateLabel.Text = "off";

            pictureButton.BorderStyle = BorderStyle.None;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }





        }
    }

